vimwiki ftp空间

all2ftp all to ftp
2ftp this file to ftp
" upload vimwiki to ftp
"
if exists("loaded_Vimwiki2Ftp")
    finish
endif
let loaded_Vimwiki2Ftp = 1

function VimwikiAll2Ftp()
python <<EOF

import vim
import os
from ftplib import FTP
import pickle

vim.command(':w')

host = 'portal.sjtu.edu.cn'
timeout = 30
port = 21
user = 'username'
psw = 'password'

ftp = FTP(host=host,user=user,passwd=psw,timeout=timeout)
print ftp.getwelcome()

wikipath = r'E:\ideas'
fctlist = {}
for root , dirs , files in os.walk(os.path.join(wikipath,'vimwiki')):
    rootftp = root[len(wikipath):].replace('\\',r'/')
    ftp.cwd('/')
    try :
        ftp.cwd(rootftp)
    except:
        ftp.cwd('/')
        ftp.mkd(rootftp)
    for f in files :
        fp = os.path.join(root,f)
        fctlist[fp] = os.stat(fp).st_mtime

fctpath = os.path.join(wikipath+os.sep+'vimwiki','fct')
print fctpath
if os.path.exists(fctpath) :
    fct = pickle.load(file(fctpath))
else :
    fct = {}
filelist = [ i for i in fctlist if (not i in fct) or fctlist[i]>fct[i] ]
pickle.dump(fctlist,file(fctpath,'w'))

for wiki in filelist :
    ftppath = wiki[len(wikipath):].replace('\\',r'/')
    ftp.storbinary("STOR "+ftppath,open(wiki,'rb'))
    print ftppath

ftp.quit()
EOF
endfunction


function Vimwiki2Ftp()
python <<EOF

import vim
import os , sys
from ftplib import FTP

vim.command(':w')

host = 'portal.sjtu.edu.cn'
timeout = 30
port = 21
user = 'username'
psw = 'password'
wikipath = r'E:/ideas'

wiki= vim.current.buffer.name
if not wiki.startswith(wikipath):
    sys.exit()
wikidir = os.path.dirname(wiki[8:])
wikiname = os.path.basename(wiki[8:])

ftp = FTP(host=host,user=user,passwd=psw,timeout=timeout)
print ftp.getwelcome()
ftp.cwd(wikidir)

#ftp.storbinary("STOR"+wikiname,open(wikipath+os.sep+wiki,'rb'))
print wiki
ftp.storbinary("STOR "+wikiname,open(wiki,'rb'))
print 'upload to ftp successful'

ftp.quit()
EOF
endfunction


" map
nmap :all2ftp  :call VimwikiAll2Ftp()<CR>
nmap :2ftp  :call Vimwiki2Ftp()<CR>